/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.form;

import domen.Korisnik;
import java.util.regex.PatternSyntaxException;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import model.Storage;
import model.StorageBroker;
import ui.components.modeltabele.KorisniciModelTabele;

/**
 *
 * @author Vlada
 */
public class JPanelKorisniciPrikaz extends javax.swing.JPanel {

    /**
     * Creates new form JPanelKorisnikPrikaz
     */
    public JPanelKorisniciPrikaz() {
        Korisnik k = new Korisnik();
        sb = new StorageBroker(k);
        initComponents();
        KorisniciModelTabele kmt = new KorisniciModelTabele();
        jtblKorisnici.setModel(kmt);
        sortiraj();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbtnDetalji = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtblKorisnici = new javax.swing.JTable();
        jbtnObrisi = new javax.swing.JButton();
        jtfPretraga = new javax.swing.JTextField();

        addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                formFocusGained(evt);
            }
        });

        jbtnDetalji.setText("Detalji");
        jbtnDetalji.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnDetaljiActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(jtblKorisnici);

        jbtnObrisi.setText("Obrisi");
        jbtnObrisi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnObrisiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
                    .addComponent(jtfPretraga))
                .addGap(18, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jbtnObrisi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtnDetalji, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jbtnDetalji)
                    .addComponent(jtfPretraga, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jbtnObrisi)))
                .addContainerGap(26, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnDetaljiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDetaljiActionPerformed

        String korIme = (String) jtblKorisnici.getModel().getValueAt(jtblKorisnici.getSelectedRow(), 3);
        Korisnik k = (Korisnik) sb.pronadjiEntitet(korIme);
        //OVO JE PISALO pozivana je klasa FKorisnik 
      // FKorisnik fk = new FKorisnik(Storage.getInstance().getGlavnaForma(), true, k);
     //  fk.setVisible(true);
        
        // zamenio sam ga sa ovim
         JPanelKorisnik jpk = new JPanelKorisnik( k);
         
        jpk.setVisible(true);
        Storage.getInstance().getGlavnaForma().ucitajPanel(jpk);
        //
        
        
        //ovo ispod se ne izvrsava nikad 
        jtblKorisnici.revalidate();
        AbstractTableModel abs = (AbstractTableModel) jtblKorisnici.getModel();
        abs.fireTableDataChanged();
    }//GEN-LAST:event_jbtnDetaljiActionPerformed

    private void jbtnObrisiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnObrisiActionPerformed

        String korIme = (String) jtblKorisnici.getModel().getValueAt(jtblKorisnici.getSelectedRow(), 3);
        if (sb.izbrisiEntitet(korIme)) {
            System.out.println("Korisnik je izbrisan");
        } else {
            System.out.println("Korisnik ne moze da se izbrise!");
        }
//       jtblKorisnici.revalidate();
        AbstractTableModel abs = (AbstractTableModel) jtblKorisnici.getModel();
        abs.fireTableDataChanged();

    }//GEN-LAST:event_jbtnObrisiActionPerformed

    private void formFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_formFocusGained
//       jtblKorisnici.revalidate();
        AbstractTableModel abs = (AbstractTableModel) jtblKorisnici.getModel();
        abs.fireTableDataChanged();
    }//GEN-LAST:event_formFocusGained


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnDetalji;
    private javax.swing.JButton jbtnObrisi;
    private javax.swing.JTable jtblKorisnici;
    private javax.swing.JTextField jtfPretraga;
    // End of variables declaration//GEN-END:variables

    StorageBroker sb;

    public void sortiraj() {
        TableModel myTableModel = jtblKorisnici.getModel();
        final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(myTableModel);
        jtblKorisnici.setRowSorter(sorter);
        jtfPretraga.getDocument().addDocumentListener(new DocumentListener() {

            private void searchFieldChangedUpdate(DocumentEvent evt) {
                String text = jtfPretraga.getText();
                if (text.length() == 0) {
                    sorter.setRowFilter(null);
                    jtblKorisnici.clearSelection();
                } else {
                    try {
                        sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                        jtblKorisnici.clearSelection();
                    } catch (PatternSyntaxException pse) {
                        pse.getMessage();
                    }
                }
            }

            @Override
            public void insertUpdate(DocumentEvent evt) {
                searchFieldChangedUpdate(evt);
            }

            @Override
            public void removeUpdate(DocumentEvent evt) {
                searchFieldChangedUpdate(evt);
            }

            @Override
            public void changedUpdate(DocumentEvent evt) {
                searchFieldChangedUpdate(evt);
            }
        });
    }

}
